package com.pbxadmin.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.google.gson.Gson;
import com.pbxadmin.cfg.WSConstants;
import com.pbxadmin.pojo.SipTrunk;
import com.pbxadmin.service.PbxWebService;

@Controller
@RequestMapping("/siptrunk")
public class SipTrunkController {

	private Logger log = LoggerFactory.getLogger(SipTrunkController.class);

	@Autowired
	private PbxWebService pbxWebService;
	@Autowired
	private Gson gson;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public ModelAndView list() {
		ModelAndView mav = new ModelAndView("/siptrunk/list");
		String returnCode = pbxWebService.invokePbx("list_sip_trunk");
		Map<String,SipTrunk> mapSipTrunk = gson.fromJson(returnCode, WSConstants.mapSipTrunk);
		mav.addObject("mapSipTrunk", mapSipTrunk);
		log.debug("siptrunk/list:{}",returnCode);
		return mav;
	}
	
	/**
	 * 修改初始化
	 */
	@RequestMapping("/modify/{name}/init")
	public ModelAndView modifyInit(HttpServletRequest req, @PathVariable("name") String name) {
		if(name == null || name.isEmpty()){
			return new ModelAndView("/error/parameters","message","/siptrunk/modify/{name}/init , name is null");
		}
		String result = req.getParameter("result");
		if(result != null){
			result = new String(Base64.decodeBase64(result));
		}else{
			result = "";
		}
		
		ModelAndView mav = new ModelAndView("/siptrunk/modify");
		String returnCode = pbxWebService.invokePbx("list_sip_trunk");
		Map<String,SipTrunk> mapSipTrunk = gson.fromJson(returnCode, WSConstants.mapSipTrunk);
		SipTrunk sipTrunk = mapSipTrunk.get(name);
		mav.addObject("name", name);
		mav.addObject("sipTrunk", sipTrunk);
		mav.addObject("result", result);
		return mav;
	}
	/**
	 * 修改
	 */
	@RequestMapping("/modify/{name}")
	public ModelAndView modify(HttpServletRequest req, @PathVariable("name") String name,@ModelAttribute SipTrunk sipTrunk) {
		if(name == null || name.isEmpty()){
			return new ModelAndView("/error/parameters","message","/siptrunk/modify/{name}/init , name is null");
		}
		
		String gsonStr = gson.toJson(sipTrunk);
		//String returnCode = WSConstants.RTN_SUCCESS;
		String returnCode = pbxWebService.invokePbx("mod_sip_trunk", new Object[]{name,gsonStr});
		String result = "";
		if(WSConstants.RTN_SUCCESS.equals(returnCode)){
			result = "修改SIP中继["+name+"]成功";
		}else{
			result = "修改SIP中继["+name+"]失败";
		}
		result = Base64.encodeBase64URLSafeString(result.getBytes());
		return new ModelAndView(new RedirectView("/pbxadmin/siptrunk/modify/"+name+"/init"),"result",result);
	}

	public PbxWebService getPbxWebService() {
		return pbxWebService;
	}

	public void setPbxWebService(PbxWebService pbxWebService) {
		this.pbxWebService = pbxWebService;
	}

	public Gson getGson() {
		return gson;
	}

	public void setGson(Gson gson) {
		this.gson = gson;
	}
}
